<?php

	include("common.php");
	
	if(!empty($_POST)){
		mysql_query("INSERT INTO `197_users` ( `username`, `password`, `email`, `type`, `realname` ) VALUES ( '" . $_POST['username'] . "', '" . md5($_POST['password']) . "', '" . $_POST['email'] . "', 1, '" . $_POST['realname'] . "' )");
		include("header.php");
		
?>
<?php if(!AJAX){ ?><h1>註冊</h1><?php } ?>
<p>註冊成功，請<a href="index.php">按此</a>返回。</p>
<?php

		include("sidebar.php");
		include("footer.php");
		exit;
		
	}
	
	if(isset($_GET['username'])){
		$result = mysql_query("SELECT * FROM `197_users` WHERE `username` = '" . $_GET['username'] . "'");
		if(!mysql_num_rows($result)){
			echo 'yes';
		} else {
			echo 'no';
		}
		exit();
	}

?>
<?php include("header.php"); ?>
<h1>註冊</h1>
<link rel="stylesheet" href="jquery/css/error-drawer.css" />
<script type="text/javascript" defer="defer">
	function regUser(){
		
		
		return $("#register-form").data("validator").checkValidity();
		
		//$("#register-result").html("<img src='./images/ajax-loader.gif' />");
		//$.post("register.php?ajax=true", { username: $("#regusername").val(), password: $("#regpassword").val(), email: $("#regemail").val(), realname: $("#regrealname").val() }, function(r){ $("#register-result").html(r) });
	}
	
	function checkUsername(){
		username = $("#regusername").val();
		$("#regusername").css("background", "url('images/ajax-loader.gif') no-repeat").val('');
		$.post("register.php?username=" + username, {}, function(r){
			if(r == "no"){
				$("#regusername").css("background", "#FF9999").val(username);
				$("#username-error").show();
				$("#regsubmit").attr('disabled', true);
			} else {
				$("#regusername").css("background", "#99FF99").val(username);
				$("#username-error").hide();
				$("#regsubmit").attr('disabled', false);
			}
		});
	}
	
	$(function(){
		// select all desired input fields and attach tooltips to them
		$("#register-form :input").tooltip({

			// place tooltip on the right edge
			position: "center right",

			// a little tweaking of the position
			offset: [-2, 10],

			// use the built-in fadeIn/fadeOut effect
			effect: 'slide',
			slideOffset: 10,
			direction: 'right',

			// custom opacity setting
			opacity: 0.7,
			
			tipClass: "input-tooltip"

		});
	});
	
	$(function(){
		$("#register-form").validator();
	});
</script>
<link rel="stylesheet" href="./jquery/css/input-tooltip.css" />
<form action="register.php" style="border: 4px #AAA solid; width: 260px; padding: 10px;" onsubmit="if(!regUser()){ return false; }" id="register-form" method="POST">
	<p><label>帳號：<input required="required" type="text" name="username" id="regusername" onblur="checkUsername();" /></label></p>
	<p id="username-error" style="display: none; color: #FF0000; font-weight: bold;">帳號已被註冊。</p>
	<p><label>密碼：<input required="required" type="password" title="建議使用6個字元以上的英文數字混合。" name="password" id="regpassword" /></label></p>
	<p><label>信箱：<input required="required" type="email" title="請務必填寫真實E-Mail，以便聯絡。" name="email" id="regemail" /></label></p>
	<p><label>姓名：<input required="required" type="text" name="realname" id="regrealname" /></label></p>
	<p><input type="submit" id="regsubmit" value="註冊" /></p>
	<div id="register-result"></div>
</form>
<?php include("sidebar.php"); ?>
<?php include("footer.php"); ?>